package io.github.enbool.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class IPUtil {
	
	public static String getIP(HttpServletRequest request) {  
	    String ip = request.getHeader("X-Forwarded-For");  
	    if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){  
	        
	        int index = ip.indexOf(",");  
	        if(index != -1){  
	            return ip.substring(0,index);  
	        }else{  
	            return ip;  
	        }  
	    }  
	    ip = request.getHeader("X-Real-IP");  
	    if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){  
	       return ip;  
	    }  
	    return request.getRemoteAddr();  
	}  

}
